<?php
	
	$TEMP->set_file(array('handle' => DIR_IFRAME_VIEW . 'scheduleSectionIrregular.html'));
	
	$STUDID = _GET('id');
	$CID 	= _GET('cid');
	$SSID 	= _GET('ssid');
	$SLID 	= _GET('slid');
	$ISR 	= _GET('isr');

	$irregularAutoGenerateSchedulesCount = $STUD->getIrregularAutoGenerateSchedulesCount($STUDID, $CID, $SLID, $activeYear);
	if ( $irregularAutoGenerateSchedulesCount > 0 ) {
		$refreshSaveBut = '';
	}
	else {
		// SECTION LSIT
		$irregularSemesterList	= $STUD->getIrregularSemesterList($STUDID, $SLID);
		if ( !empty($irregularSemesterList) ) {
			$sectionList = implode(',', $irregularSemesterList);
		}
		// SUBJECT LSIT
		$studentSubjectList = $STUD->getStudentSubjectTakenList($STUDID, $CID, $SLID);
		if ( !empty($studentSubjectList) ) {
			$subjIrregStudArr = '';
			foreach($studentSubjectList as $studentSubjectListValue) {
				$subjIrregStudArr .= $studentSubjectListValue['subj_idPK'] . ',';
			}
			$subjIrregStud = trim(rtrim($subjIrregStudArr, ','));
		}
		$refreshSaveBut = '<div class="preview-schedule">
								<div>AUTO GENERATED SUBJECT SCHEDULE :: <span class="font-green" onclick="refreshPost();">REFRESH</span> :: <span id="save_schedule" onclick="showLoading(\'' . $activeYear . '\');">SAVE</span></div>
						   </div>
						   <div class="preview-schedule display-none" id="preview_schedule_waiting">
								<div class="preview-schedule-waiting"><img src="./application/images/icon/loading_round.gif" border="0" /> Saving Schedule. Loading...</div>
						   </div>';
	}
	
	$scheduleDefaultTime = $STUD->getScheduleDefaultTime(array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'), $activeYear);
	
	$timeCount  = 0;
	$bgcolor 	= LOOPING_F0F;
	if (!empty($scheduleDefaultTime)) {
		$scheduleHTML = $refreshSaveBut . '<table width="0" border="0" cellspacing="0" cellpadding="0" class="schedule-table">
						 <tr>
							<td width="120" height="25" class="schedule-label schedule-label-right" colspan="3">&nbsp;</td>
							<td width="105" class="schedule-label schedule-label-right">Monday</td>
							<td width="105" class="schedule-label schedule-label-right">Tuesday</td>
							<td width="105" class="schedule-label schedule-label-right">Wenesday</td>
							<td width="105" class="schedule-label schedule-label-right">Thursday</td>
							<td width="105" class="schedule-label schedule-label-right">Friday</td>
							<td width="105" class="schedule-label">Saturday</td>
						 </tr>';
						 
		$timeScheduleFrom = $STUD->getTimeSchedule('sched_from', $activeYear, 1);
		$timeScheduleTo   = $STUD->getTimeSchedule('sched_to', $activeYear, 1);
		
		unset($_SESSION['irreg_schedule']);
		$resultIrregular = array();
		$timeYear 		 = $STUD->getTimeSchedule('sched_time', $activeYear);
		$subjArr 		 = array();
		foreach($scheduleDefaultTime as $scheduleDayField => $scheduleDay) {
			$scheduleHTML .= '<tr align="center" ' . $bgcolor . '><td width="50" class="schedule-label-time"><div>' . date("h:i", strtotime($timeScheduleFrom[$timeCount])) . '</div><div style="font-weight:normal; font-style:italic; font-size:12px;">' . date("a", strtotime($timeScheduleFrom[$timeCount])) . '</div></td><td width="20" class="schedule-label-time"> - </td><td width="50" class="schedule-label-time schedule-label-right"><div>' . date("h:i", strtotime($timeScheduleTo[$timeCount])) . '</div><div style="font-weight:normal; font-style:italic; font-size:12px;">' . date("a", strtotime($timeScheduleTo[$timeCount])) . '</div></td>';
			$rightCount = 1;
			$countDay   = count($scheduleDay);
			foreach($scheduleDay as $scheduleTimeField => $scheduleTime) {
				$clasRight = ($rightCount == $countDay) ? '' : 'schedule-time-right';
				if ($scheduleTime['sched_break'] == 'Y') {
					$sched = '<span class="font-red"><strong>LUNCH</strong></span>';
				}
				else {
					
					$sectionSubjectGenerateSchedules = ( $irregularAutoGenerateSchedulesCount > 0 ) ? $STUD->getIrregularSubjectGenerateSchedules($STUDID, $CID, $SLID, $activeYear, $scheduleTime['sched_time_idPK'])
																									: $STUD->getIrregularSubjectGenerateAutoSchedules($sectionList, $subjIrregStud, $scheduleTime['sched_time_idPK']);
					
					$getSubject = $DSC->getSubject($sectionSubjectGenerateSchedules['subj_idPK']);
														
					if ( $subjArr[$sectionSubjectGenerateSchedules['subj_idPK']] < $getSubject['total_hrs'] ) {
						
						$subjArr[$sectionSubjectGenerateSchedules['subj_idPK']] += $timeYear[0];
					
						$sched = '';
						if ( !empty($sectionSubjectGenerateSchedules) ) {
							//r_print($sectionSubjectGenerateSchedules);
							$resultIrregular[] = array('stud_idFK' 				=> $STUDID,
														'course_idFK' 			=> $CID,
														'sem_linker_idFK' 		=> $SLID,
														'year_idFK' 			=> $activeYear,
														'subj_idFK' 			=> $sectionSubjectGenerateSchedules['subj_idPK'],
														'prof_subj_sched_idFK' 	=> $sectionSubjectGenerateSchedules['prof_subj_sched_idPK']);
							
							switch($sectionSubjectGenerateSchedules['lec_lab_both']) {
								case '0': $lecLabBoth = '(<em>lec</em>)'; break;
								case '1': $lecLabBoth = '(<em>lab</em>)'; break;
								case '2': $lecLabBoth = '(<em>lec/lab</em>)'; break;
							}
							if ( $sectionSubjectGenerateSchedules['is_saturday'] == 'N' ) {
								$sched .= '<div><strong>' . upperCase(substr($sectionSubjectGenerateSchedules['first_name'],0,1)) . '. ' . upperCase($sectionSubjectGenerateSchedules['last_name']) . '</strong></div>';
								$sched .= '<div>' . $sectionSubjectGenerateSchedules['room_desc'] . '</div>';
								$sched .= '<div>' . $sectionSubjectGenerateSchedules['section_desc'] . '</div>';
								$sched .= '<div>' . $lecLabBoth . '</div>';
							}
							$sched .= '<div><strong>' . $sectionSubjectGenerateSchedules['subj_code'] . '</strong></div>';
						}
						else {
							$sched = '&nbsp;';
						}
					}
					else {
						$sched = '&nbsp;';
					}
				}
				$scheduleHTML .= '<td class="schedule-time ' . $clasRight . '" height="85" align="center">' . $sched . '</td>';
				$rightCount++;
			}
			//r_print($subjArr);
			$scheduleHTML .= '</tr>';
			$bgcolor = ($bgcolor == LOOPING_F0F) ? LOOPING_FFF : LOOPING_F0F;
			$timeCount++;
		}
		$scheduleHTML .= '<tr><td height="5" class="schedule-label-right" colspan="3"></td><td colspan="6"></td></tr></table>';
	}
	else {
		$scheduleHTML = '<div class="change-section-button"><div class="set-time-schedule" onclick="setTimeSchedule(\'' . $activeYear . '\');">SET TIME FOR MONDAY - SATURDAY SCHEDULE</div></div><div id="set_time_schedule"></div>';
	}
	/* - - - - - - - */
	
	$_SESSION['irreg_schedule'] = $resultIrregular;
	
	$getCourse 	  = $DSC->getCourse($CID);
	if(!empty($getCourse)) {
		$course_label	= $getCourse['course_desc'] . ' (' . $getCourse['course_code'] . ')';
	}
	$TEMP->set_var(array('prof_name' 		=> $profName,
						 'time_table'		=> $scheduleHTML,
						 'save_sched'		=> $saveSched,
						 'cid'				=> $CID,
						 'course_label'		=> $course_label,
						 'section_html'		=> $sectionHTML,
						 'section_desc'		=> $sectionDesc,
						 'sy_label'			=> $__SYR__
						));
?>